<?php

namespace App\Http\Controllers\Admin\UserInfos;

use App\Http\Models\Admin\AdminUsers;
use App\Http\Responses\ResponseDto;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Psy\Util\Json;

class UserInfosController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        return View::make('admin.userinfo.user_message')->withUserinfo($request->session()->get('adminUser'))
            ->withUrltitle(trans("admin/message.ret_msg"))
            ->withAssets($request['ASSETS']);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function savemessage(Request $request)
    {
        $validator = Validator::make(Input::all(),
            [
//                'nick_name' => 'required',
                'email' => 'required',
            ]);
        if($validator->fails()){
            return ResponseDto::createResObj($validator->errors()->first(),ResponseDto::$FAIL);
        }else{
            $seuser = $request->session()->get("adminUser");
            $aduser = AdminUsers::find($seuser->id);
//            $aduser->nick_name = $request->input("nick_name");
            $aduser->email = $request->input("email");

            $aduser->save();
            $request->session()->put("adminUser",$aduser::find($aduser->id));
            return ResponseDto::createResObj(trans("admin/message.update_success"),ResponseDto::$SUCCESS);
        }
    }

    /*
     * 重置密码
     * */
    public function resetpwd(Request $request)
    {
        $validator = Validator::make(Input::all(),
            [
                'newPassword' => 'required|min:6|confirmed',
                'newPassword_confirmation' => 'required|min:6',
            ]);

        if($validator->fails()){
            return ResponseDto::createResObj($validator->errors()->first(),ResponseDto::$FAIL);
        }else{

            $aduser = $request->session()->get("adminUser");

            $fin = AdminUsers::where('id', $aduser->id)->where('password', md5($request->input("oldPassword")))->first();
            if($fin == null){
                return ResponseDto::createResObj(trans("admin/message.password_wrong"),ResponseDto::$FAIL);
            }else{

                $nuser = AdminUsers::find($aduser->id);
                if($nuser){
                    $nuser->password = md5($request->input("newPassword"));
                    $nuser->save();

                    $request->session()->set("adminUser",$nuser);
                    return ResponseDto::createResObj(trans("admin/message.update_success"),ResponseDto::$SUCCESS);
                }
                return ResponseDto::createResObj(trans("admin/message.server_fail"),ResponseDto::$FAIL);

            }
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function savephoto(Request $request)
    {
        //

        $aduser = $request->session()->get("adminUser");
        $user = AdminUsers::find($aduser->id);
        if($user){
            $user->photo_url = $request->input("url");
            $aduser->photo_url = $user->photo_url;
            $user->save();
        }
        return ResponseDto::createResObj(trans("admin/message.update_success"),ResponseDto::$SUCCESS);
    }


}
